package cn.labbook.bluetooth.util;

import cn.labbook.bluetooth.common.SmsTemplateEnum;
import cn.labbook.bluetooth.util.validate.MobileUtils;
import cn.labbook.bluetooth.util.weixin.WeiXinRequestUtils;
import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import com.aliyuncs.profile.IClientProfile;

import java.util.Random;

public class MessageUtil {


    /**
     * Created by Administrator on 2019/3/5.
     */
    /*发送验证码*/
    public static String sendShortMessage(String phone,String code) {

        Random random = new Random();
        StringBuilder result = new StringBuilder();
        for (int i = 0; i < 6; i++) {
            result.append(random.nextInt(10));
        }
        int i = Integer.parseInt(result.toString());

        DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou", "LTAI9s79vleJpwII", "uKUA0vWPhgvj28THpJC1o67g82NVwF");
        IAcsClient client = new DefaultAcsClient(profile);

        CommonRequest request = new CommonRequest();
        //request.setProtocol(ProtocolType.HTTPS);
        request.setMethod(MethodType.POST);
        request.setDomain("dysmsapi.aliyuncs.com");
        request.setVersion("2017-05-25");
        request.setAction("SendSms");
        request.putQueryParameter("RegionId", "cn-hangzhou");
        request.putQueryParameter("PhoneNumbers", phone);
        request.putQueryParameter("SignName", "佐小二");
        request.putQueryParameter("TemplateCode", code);
        request.putQueryParameter("TemplateParam", "{\"code\":\"" + result.toString() + "\"}");

        try {
            CommonResponse response = client.getCommonResponse(request);
            System.out.println(response.getData());
        } catch (ClientException e) {
            e.printStackTrace();
        }

        return result.toString();
    }

    /*发送绑定电表通知*/
    public static String sendBindingMessage(String mobilPhone,String phone,String name,String code) {
        Random random = new Random();
        StringBuilder result = new StringBuilder();
        for (int i = 0; i < 6; i++) {
            result.append(random.nextInt(10));
        }
        int i = Integer.parseInt(result.toString());

        DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou", "LTAI9s79vleJpwII", "uKUA0vWPhgvj28THpJC1o67g82NVwF");
        IAcsClient client = new DefaultAcsClient(profile);

        CommonRequest request = new CommonRequest();
        //request.setProtocol(ProtocolType.HTTPS);
        request.setMethod(MethodType.POST);
        request.setDomain("dysmsapi.aliyuncs.com");
        request.setVersion("2017-05-25");
        request.setAction("SendSms");
        request.putQueryParameter("RegionId", "cn-hangzhou");
        request.putQueryParameter("PhoneNumbers", mobilPhone);
        request.putQueryParameter("SignName", "佐小二");
        request.putQueryParameter("TemplateCode", code);
        request.putQueryParameter("TemplateParam", "{\"name\":\""+name+"\",\"phone\":\""+phone+"\",\"num\":\""+result.toString()+"\"}");

        try {
            CommonResponse response = client.getCommonResponse(request);
            System.out.println(response.getData());
        } catch (ClientException e) {
            e.printStackTrace();
        }

        return result.toString();
    }

    public static String sendFailMessage(String mobilePhone, String msg, String code) {
        StringBuilder result = new StringBuilder();
        DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou", "LTAI9s79vleJpwII", "uKUA0vWPhgvj28THpJC1o67g82NVwF");
        DefaultAcsClient defaultAcsClient = new DefaultAcsClient(profile);
        CommonRequest request = new CommonRequest();
        request.setMethod(MethodType.POST);
        request.setDomain("dysmsapi.aliyuncs.com");
        request.setVersion("2017-05-25");
        request.setAction("SendSms");
        request.putQueryParameter("RegionId", "cn-hangzhou");
        request.putQueryParameter("PhoneNumbers", mobilePhone);
        request.putQueryParameter("SignName", "佐小二");
                request.putQueryParameter("TemplateCode", code);
        request.putQueryParameter("TemplateParam", "{\"msg\":\"" + msg + "\"}");
        try {
            CommonResponse response = defaultAcsClient.getCommonResponse(request);
            System.out.println(response.getData());
        } catch (ClientException e) {
            e.printStackTrace();
        }
        return result.toString();
    }

    /*发送退房退款通知*/
    public static String sendUntyingMessage(String phone,String name,Double amount,Double balance,String code) {
        Random random = new Random();
        StringBuilder result = new StringBuilder();
        for (int i = 0; i < 6; i++) {
            result.append(random.nextInt(10));
        }
        int i = Integer.parseInt(result.toString());

        DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou", "LTAI9s79vleJpwII", "uKUA0vWPhgvj28THpJC1o67g82NVwF");
        IAcsClient client = new DefaultAcsClient(profile);

        CommonRequest request = new CommonRequest();
        //request.setProtocol(ProtocolType.HTTPS);
        request.setMethod(MethodType.POST);
        request.setDomain("dysmsapi.aliyuncs.com");
        request.setVersion("2017-05-25");
        request.setAction("SendSms");
        request.putQueryParameter("RegionId", "cn-hangzhou");
        request.putQueryParameter("PhoneNumbers", phone);
        request.putQueryParameter("SignName", "佐小二");
        request.putQueryParameter("TemplateCode", code);
        request.putQueryParameter("TemplateParam", "{\"name\":\""+name+"\",\"amount\":\""+amount+"\",\"balance\":\""+balance+"\",\"code\":\""+result.toString()+"\"}");

        try {
            CommonResponse response = client.getCommonResponse(request);
            System.out.println(response.getData());
        } catch (ClientException e) {
            e.printStackTrace();
        }

        return result.toString();
    }

    public static void sendApplyMessage(String phone, String code, String address) {
        if (MobileUtils.isNotValidChinesePhone(phone)) {
            return;
        }

        String scheme = WeiXinRequestUtils.getScheme();
        if (StringUtils.isBlank(scheme)) {
            return;
        }


        DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou", "LTAI9s79vleJpwII", "uKUA0vWPhgvj28THpJC1o67g82NVwF");
        IAcsClient client = new DefaultAcsClient(profile);

        CommonRequest request = new CommonRequest();
        request.setMethod(MethodType.POST);
        request.setDomain("dysmsapi.aliyuncs.com");
        request.setVersion("2017-05-25");
        request.setAction("SendSms");
        request.putQueryParameter("RegionId", "cn-hangzhou");
        request.putQueryParameter("PhoneNumbers", phone);
        request.putQueryParameter("SignName", "佐小二");
        request.putQueryParameter("TemplateCode", code);

        String url = "bluetooth/v2/h5/jump?scheme=" + scheme + " ";
        request.putQueryParameter("TemplateParam", "{\"address\":\""+address+"\",\"code\":\""+url+"\"}");

        try {
            CommonResponse response = client.getCommonResponse(request);
            System.out.println(response.getData());
        } catch (ClientException e) {
            e.printStackTrace();
        }
    }

    public static void sendLandlordRemindMessage(String phone, String code, String url) {
        if (MobileUtils.isNotValidChinesePhone(phone)) {
            return;
        }

        DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou", "LTAI9s79vleJpwII", "uKUA0vWPhgvj28THpJC1o67g82NVwF");
        IAcsClient client = new DefaultAcsClient(profile);

        CommonRequest request = new CommonRequest();
        request.setMethod(MethodType.POST);
        request.setDomain("dysmsapi.aliyuncs.com");
        request.setVersion("2017-05-25");
        request.setAction("SendSms");
        request.putQueryParameter("RegionId", "cn-hangzhou");
        request.putQueryParameter("PhoneNumbers", phone);
        request.putQueryParameter("SignName", "佐小二");
        request.putQueryParameter("TemplateCode", code);


        request.putQueryParameter("TemplateParam", "{\"code\":\""+url+"\"}");

        try {
            CommonResponse response = client.getCommonResponse(request);
            System.out.println(response.getData());
        } catch (ClientException e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        String accessToken = WeiXinRequestUtils.getAccessToken();
        if (StringUtils.isBlank(accessToken)) {
            return;
        }

        String scheme = WeiXinRequestUtils.getScheme(accessToken);
        if (StringUtils.isBlank(scheme)) {
            return;
        }
        String url = "bluetooth/v2/h5/jump?scheme=" + scheme + " ";
        sendLandlordRemindMessage("15158165920", SmsTemplateEnum.LANDLORD_REMIND.getCode(), url);
    }
}
